package zjut.raymond.witdemo.ui.view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PointF;
import android.util.AttributeSet;
import android.util.Log;

import androidx.core.content.ContextCompat;

import com.davemorrissey.labs.subscaleview.SubsamplingScaleImageView;

import java.util.ArrayList;
import java.util.List;

import zjut.raymond.witdemo.R;
import zjut.raymond.witdemo.ui.DataFragment;

public class MapView extends SubsamplingScaleImageView {
    private static Paint dotPaint;
  //  private static Paint fusiondotPaint;
    private static Paint wifidotPaint;
    private static Paint linePaint;
    private static Paint pdrPaint;
 //   private static Paint fusionPaint;
    private static Paint labelPaint;
    private static Paint locationBitmapPaint;
 //   private static Paint fusionlocationBitmapPaint;

    private final Paint paint = new Paint();

    private static Bitmap locationBitmap;
//    private static Bitmap fusionLocationBitmap;
    private static Matrix locationBitmapMatrix;
 //   private static Matrix fusionlocationBitmapMatrix;

    private PointF pdrLocation;
    private PointF wifiLocation;
 //   private PointF fusionLocation;
    private float currentOrientation;

    private static List<PointF> referencePoints = new ArrayList<>();
    private static List<PointF> truePaths = new ArrayList<>();
    private List<PointF> pdrPath = new ArrayList<>();
 //   private List<PointF> fusionPath = new ArrayList<>();
    private List<PointF> wifiLocationResults = new ArrayList<>();


    private boolean isDrawReferencePoint = true;

    static {
        for (int i = 0; i < 10; i++) {
            referencePoints.add(new PointF(745, 2978 + i * 108));
        }
        for (int i = 1; i <= 15; i++) {
            referencePoints.add(new PointF(745 + i * 108, 3950));
        }
        // truePaths.add(new PointF(2005, 3950));
        truePaths.add(new PointF(745, 3950));
        truePaths.add(new PointF(745, 3050));
    }

    public MapView(Context context, AttributeSet attr) {
        super(context, attr);
        initialise();
    }

    public MapView(Context context) {
        super(context);
    }

    private void initialise() {
        paintInitial();
        setDoubleTapZoomDpi(100);
        setMinimumScaleType(SCALE_TYPE_CENTER_CROP);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        // Don't draw pin before image is ready so it doesn't move around during setup.
        if (!isReady()) {
            return;
        }

        paint.setAntiAlias(true);

        if (isDrawReferencePoint) {
            //绘制参考点位置
//            for (int i = 0; i < referencePoints.size(); i++) {
//                PointF p = referencePoints.get(i);
//                p = sourceToViewCoord(p.x, p.y);
//                if (p != null) {
//                    dotPaint.setColor(ContextCompat.getColor(getContext(),R.color.colorPrimaryDark));
//                    dotPaint.setStrokeWidth(10);
//                    dotPaint.setAlpha(255);
//                    canvas.drawCircle(p.x, p.y, 0.5F, dotPaint);
//                    labelPaint.setFakeBoldText(true);
//                    canvas.drawText(i + 1 + "", p.x, p.y + labelPaint.getTextSize() * 1.5f, labelPaint);
//                }
//            }

            //绘制真实路径
            Path path = new Path();
            PointF start = sourceToViewCoord(2005, 3950);
            PointF turn = sourceToViewCoord(745, 3950);
            PointF stop = sourceToViewCoord(745, 3050);
            if (start != null && turn != null && stop != null) {
                canvas.drawText("START/STOP", start.x, start.y + labelPaint.getTextSize() * 1.5f, labelPaint);
                canvas.drawCircle(start.x, start.y, 0.25F, dotPaint);
                canvas.drawText("TURN", turn.x, turn.y + labelPaint.getTextSize() * 1.5f, labelPaint);
                canvas.drawCircle(turn.x, turn.y, 0.25F, dotPaint);
                canvas.drawText("TURN ROUND", stop.x, stop.y - labelPaint.getTextSize(), labelPaint);
                canvas.drawCircle(stop.x, stop.y, 0.25F, dotPaint);
                path.moveTo(start.x, start.y);
                path.lineTo(turn.x, turn.y);
                path.lineTo(stop.x, stop.y);
                canvas.drawPath(path, linePaint);
            }

//            PointF start = sourceToViewCoord(1796, 3950);
//            canvas.drawCircle(start.x, start.y, 0.25f, dotPaint);
        }


        if (pdrLocation != null) {
            PointF pointF = DataFragment.mRouter.get();
            if (pointF != null) {
                PointF p = sourceToViewCoord(pointF);
                locationBitmapPaint.setAlpha(255);
                locationBitmapMatrix.reset();
                locationBitmapMatrix.setTranslate(p.x - locationBitmap.getWidth() / 2, p.y - locationBitmap.getHeight() / 2);
                locationBitmapMatrix.postRotate(currentOrientation, p.x, p.y);
                canvas.drawBitmap(locationBitmap, locationBitmapMatrix, locationBitmapPaint);
            }
        }
//        if (fusionLocation != null) {
//           // PointF pointF = DataFragment.fusionRouter.get();
//          //  if (pointF != null) {
//                PointF p = sourceToViewCoord(fusionLocation);
//                fusionlocationBitmapPaint.setAlpha(255);
//                fusionlocationBitmapMatrix.reset();
//                fusionlocationBitmapMatrix.setTranslate(p.x - fusionLocationBitmap.getWidth() / 2, p.y - fusionLocationBitmap.getHeight() / 2);
//                fusionlocationBitmapMatrix.postRotate(currentOrientation, p.x, p.y);
//                canvas.drawBitmap(fusionLocationBitmap, fusionlocationBitmapMatrix, fusionlocationBitmapPaint);
//           // }
//        }

        //画路线
        if (pdrPath.size() > 1) {
        //    canvas.drawText(pdrPath.size() + " ", 100, 100, labelPaint);
            Path path = new Path();
            PointF start = sourceToViewCoord(2005, 3950);
            if (start != null) {
                canvas.drawCircle(start.x, start.y, 0.5F, dotPaint);
                path.moveTo(start.x, start.y);
            }

            for (int i = 1; i < pdrPath.size(); i++) {
                PointF p = pdrPath.get(i);
                p = sourceToViewCoord(p);
                Log.d("TAG", "onDraw: line to" + i + " " + p.x + " " + p.y);
                if (p != null) {
                    path.lineTo(p.x, p.y);
                }
            }
            canvas.drawPath(path, pdrPaint);
        }

        //画路线
//        if (fusionPath.size() > 1) {
//          //  canvas.drawText(pdrPath.size() + " ", 100, 100, labelPaint);
//            Path path = new Path();
//            PointF start = sourceToViewCoord(fusionPath.get(0));
//            if (start != null) {
//                canvas.drawCircle(start.x, start.y, 0.5F, fusiondotPaint);
//                path.moveTo(start.x, start.y);
//            }
//
//            for (int i = 1; i < fusionPath.size(); i++) {
//                PointF p = fusionPath.get(i);
//                p = sourceToViewCoord(p);
//                Log.d("TAG", "onDraw: line to" + i + " " + p.x + " " + p.y);
//                if (p != null) {
//                    path.lineTo(p.x, p.y);
//                }
//            }
//            canvas.drawPath(path, fusionPaint);
//        }

//        if (wifiLocation != null) {
//            PointF point0 = sourceToViewCoord(wifiLocation);
//            canvas.drawCircle(point0.x, point0.y, 0.5F, dotPaint);
//            canvas.drawText("wifi loc", point0.x, point0.y + labelPaint.getTextSize(), labelPaint);
//        }
        //绘制wifi定位结果
//        if (wifiLocationResults.size() > 0) {
//            for (PointF p : wifiLocationResults) {
//                p = sourceToViewCoord(p);
//                if (null != p) {
//                    canvas.drawCircle(p.x, p.y, 0.5f, wifidotPaint);
//                }
//            }
//        }
    }

    public void updatePDRLocation(PointF loc) {
        pdrLocation = loc;
        pdrPath.add(new PointF(loc.x, loc.y));
//        pdrRouter.set(loc);
        invalidate();
    }

//    public void updateFusionLocation(PointF loc) {
//        fusionLocation = loc;
//        fusionPath.add(new PointF(loc.x, loc.y));
////        pdrRouter.set(loc);
//        invalidate();
//    }

    public void updateOrientation(float degree) {
        currentOrientation = degree;
        invalidate();
    }

    public void updateWifiLocation(PointF loc) {
        wifiLocation = loc;
        wifiLocationResults.add(new PointF(loc.x, loc.y));
//        pdrRouter.set(loc);
        invalidate();
    }

    public void clearLocationPath() {
        pdrPath.clear();
        wifiLocationResults.clear();
    }

    private void paintInitial() {
        float density = getResources().getDisplayMetrics().densityDpi;
        int strokeWidth = (int) (density / 60f);

        // RPs Paint
        dotPaint = new Paint();
//        dotPaint.setColor(ContextCompat.getColor(getContext(), R.color.colorPrimary));
        dotPaint.setColor(ContextCompat.getColor(getContext(), R.color.colorRed));
        dotPaint.setAlpha(255);
        dotPaint.setStrokeWidth(strokeWidth << 1);
        dotPaint.setStyle(Paint.Style.STROKE);
        dotPaint.setStrokeCap(Paint.Cap.ROUND);
        dotPaint.setAntiAlias(true);
        // RPs Paint
//        fusiondotPaint = new Paint();
////        dotPaint.setColor(ContextCompat.getColor(getContext(), R.color.colorPrimary));
//        fusiondotPaint.setColor(ContextCompat.getColor(getContext(), R.color.colorRed));
//        fusiondotPaint.setAlpha(255);
//        fusiondotPaint.setStrokeWidth(strokeWidth << 1);
//        fusiondotPaint.setStyle(Paint.Style.STROKE);
//        fusiondotPaint.setStrokeCap(Paint.Cap.ROUND);
//        fusiondotPaint.setAntiAlias(true);

        // RPs Paint
        wifidotPaint = new Paint();
//        dotPaint.setColor(ContextCompat.getColor(getContext(), R.color.colorTextLabel));
        wifidotPaint.setColor(ContextCompat.getColor(getContext(), R.color.colorTextLabel));
        wifidotPaint.setAlpha(255);
        wifidotPaint.setStrokeWidth(strokeWidth << 1);
        wifidotPaint.setStyle(Paint.Style.STROKE);
        wifidotPaint.setStrokeCap(Paint.Cap.ROUND);
        wifidotPaint.setAntiAlias(true);

        //line
        linePaint = new Paint();
        linePaint.setColor(ContextCompat.getColor(getContext(), R.color.colorRed));
        linePaint.setAlpha(255);
        linePaint.setStrokeWidth(strokeWidth >> 1);
        linePaint.setStyle(Paint.Style.STROKE);
        linePaint.setStrokeCap(Paint.Cap.ROUND);
        linePaint.setAntiAlias(true);

        //pdrLine
        pdrPaint = new Paint();
        pdrPaint.setColor(ContextCompat.getColor(getContext(), R.color.colorTextLabel));
        pdrPaint.setAlpha(255);
        pdrPaint.setStrokeWidth(strokeWidth >> 1);
        pdrPaint.setStyle(Paint.Style.STROKE);
        pdrPaint.setStrokeCap(Paint.Cap.ROUND);
        pdrPaint.setAntiAlias(true);

        //fusionLine
//        fusionPaint = new Paint();
//        fusionPaint.setColor(ContextCompat.getColor(getContext(), R.color.colorGreen));
//        fusionPaint.setAlpha(255);
//        fusionPaint.setStrokeWidth(strokeWidth >> 1);
//        fusionPaint.setStyle(Paint.Style.STROKE);
//        fusionPaint.setStrokeCap(Paint.Cap.ROUND);
//        fusionPaint.setAntiAlias(true);

        // Text
        labelPaint = new Paint();
//        labelPaint.setTextSize(16);
        //     labelPaint.setColor(ContextCompat.getColor(getContext(), R.color.colorNoteLabel));
        labelPaint.setTextSize(20);
        labelPaint.setStrokeWidth(strokeWidth >> 2);
        labelPaint.setColor(ContextCompat.getColor(getContext(), R.color.colorRed));
        labelPaint.setTextAlign(Paint.Align.CENTER);
        labelPaint.setAntiAlias(true);

        locationBitmapPaint = new Paint();
        locationBitmapPaint.setAntiAlias(true);
        locationBitmapMatrix = new Matrix();
        locationBitmap = BitmapFactory.decodeResource(this.getResources(), R.drawable.ic_pdr_location);

//        fusionlocationBitmapPaint = new Paint();
//        fusionlocationBitmapPaint.setAntiAlias(true);
//        fusionlocationBitmapMatrix = new Matrix();
//        fusionLocationBitmap = BitmapFactory.decodeResource(this.getResources(), R.drawable.ic_fusion_location);
    }
}
